﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using filesquirrel.org;

namespace FileSquirrel.Logic
{
    public class XMLComparer
    {
        private FileInfo libfile;
        private FileInfo libfile_ign;

        public XMLComparer()
        {
            libfile = new FileInfo(Path.Combine(ConfigurationManager.AppSettings["LibraryDir"], ConfigurationManager.AppSettings["LibraryName"]) + ".xml");
            libfile_ign = new FileInfo(Path.Combine(ConfigurationManager.AppSettings["LibraryDir"], ConfigurationManager.AppSettings["LibraryName"]) + "_Ignore.xml");
        }

        public Library CompareExlusion()
        {
            Library myLib = Library.Load(libfile.FullName);
            Library FilteredLib = new Library();
            if (libfile_ign.Exists)
            {
                Ignore_Library myIgnoreLib = Ignore_Library.Load(libfile_ign.FullName);

                var IgnoredPaths = from p2 in myIgnoreLib.Files
                                   select p2.Path;

                foreach (Library.FilesLocalType Fileitem in myLib.Files.Where(x => !IgnoredPaths.Contains(x.Path)))
                {
                    FilteredLib.Files.Add(Fileitem);
                }
            }
            else
            {
                FilteredLib = myLib;
                libfile_ign.Create();
            }

            return FilteredLib;
        }
    }
}